<?php
/**
 * Created by 王跃达.
 * User: 小马出行
 * Date: 2017/8/24
 * Time: 16:17
 */

namespace app\api\logic;


use app\api\model\CouponModel;

use think\Model;

class CouponLogic extends Model
{


    public function __construct()
    {
        parent::__construct();

    }

    /*
     * 获取uid优惠券
     */
    public function getCouponByUid($uid)
    {
        $couponModel = new CouponModel();
        $coupon = $couponModel->getCouponByUid($uid);
        if(!empty($coupon)){
            $coupon =$this->couponDesc($coupon);
        }
        return $coupon;
    }

    /**
     * 根据优惠券价格从大到小排序
     */
    public function couponDesc($coupon)
    {
        usort($coupon, function ($a, $b) {
            $a_pirce = $a['coupon_price'];
            $b_pirce = $b['coupon_price'];
            if ($a_pirce == $b_pirce)
                return 0;
            return ($a_pirce > $b_pirce) ? -1 : 1;
        });
        return $coupon;
    }

}